/**
 * 
 */
package Exercise;

import java.util.Arrays;

/**
 * @author zhang
 * 数列 1,2
 * 1,2,2,1,1,2,1...
 */
public class SouhuKolakoskiSeq {

	public static int[] getSeq(int[] arr, int n) {
		if (arr == null || arr.length <= 0)
			return null;
		int[] result = new int[n];
		int index = 0;
		for (int i = 0; i < arr[0] && index < n; i++)
			result[index++] = arr[0];
		int start = 1;
		if (arr[0] == 1) {
			start = 2;
			if (n > 1)
				for (int i = 0; i < arr[1] && index < n; i++)
					result[index++] = arr[1];
		}
		int cur = start;
		start = start % arr.length;
		for (int i = start; index < n;) {
			for (int j = 0; j < result[cur] && index < n; j++)
				result[index++] = arr[i];
			i = (i + 1) % arr.length;
			cur++;
		}
		return result;
	}

	public static void main(String[] args) {
		int[] array = { 2, 1, 3, 1 };
		System.out.println(Arrays.toString(getSeq(array, 30)));
	}

}
